﻿#include "AmapView.h"
#include "AmapViewImpl.h"

AmapView::AmapView() : m_obLastPoint(DPointZero), m_obLastContentSize(DSizeZero), _impl(new AmapViewImpl(this)), m_pAmapViewDelegate(NULL)
{

}

AmapView::~AmapView()
{
	CAScheduler::unschedule(schedule_selector(AmapView::update), this);

	CC_SAFE_RELEASE_NULL(_impl);
}

AmapView* AmapView::createWithFrame(const DRect& rect)
{
	AmapView* amap = new AmapView();
	if (amap && amap->initWithFrame(rect))
	{
		amap->autorelease();
		return amap;
	}
	CC_SAFE_DELETE(amap);
	return NULL;
}

AmapView* AmapView::createWithCenter(const DRect& rect)
{
	AmapView* amap = new AmapView();
	if (amap && amap->initWithCenter(rect))
	{
		amap->autorelease();
		return amap;
	}
	CC_SAFE_DELETE(amap);
	return NULL;
}

AmapView* AmapView::createWithLayout(const DLayout& layout)
{
	AmapView* amap = new AmapView();
	if (amap && amap->initWithLayout(layout))
	{
		amap->autorelease();
		return amap;
	}
	CC_SAFE_DELETE(amap);
	return NULL;
}

void AmapView::setMapCenter(double lat, double lng)
{
	_impl->setMapCenter(lat, lng);
}

void AmapView::startLocation()
{
	_impl->startLocation();
}

void AmapView::setMapZoomLevel(float level)
{
	_impl->setMapZoomLevel(level);
}

void AmapView::onEnterTransitionDidFinish()
{
	CAScheduler::schedule(schedule_selector(AmapView::update), this, 1 / 60.0f);
	CAView::onEnterTransitionDidFinish();
}

void AmapView::onExitTransitionDidStart()
{
	CAScheduler::unschedule(schedule_selector(AmapView::update), this);
	_impl->setVisible(false);
	CAView::onExitTransitionDidStart();
}

void AmapView::setVisible(bool visible)
{
	CAView::setVisible(visible);
	_impl->setVisible(visible);
}

void AmapView::addMarker(AmapMarker* marker)
{
	_impl->addMarker(marker);
}

void AmapView::deleteMarker(AmapMarker* marker)
{
	_impl->deleteMarker(marker);
}

void AmapView::clearMarkers()
{
	_impl->clearMarkers();
}

void AmapView::update(float dt)
{
	do
	{
		DPoint point = this->convertToWorldSpace(m_obPoint);
		DSize contentSize = this->convertToWorldSize(m_obContentSize);
		CC_BREAK_IF(m_obLastPoint.equals(point) && m_obLastContentSize.equals(contentSize));
		m_obLastPoint = point;
		m_obLastContentSize = contentSize;

		_impl->update(dt);

	} while (0);
}

std::map<string, AmapMarker*>& AmapView::getAllMarkers()
{
	return _impl->getAllMarkers();
}
